|
mruby 4.0.0
mruby is the lightweight implementation of the Ruby language
|
This guide describes the Ruby language features supported by mruby 4.0. mruby implements a subset of the Ruby language, optimized for embedded use. For a list of specific behavioral differences, see limitations.md.
If you are coming from CRuby, note these major differences upfront:
See Key Differences from CRuby for the full list.
mruby supports the following keywords:
BEGIN, END, alias, and, begin, break, case, class, def, do, else, elsif, end, ensure, false, for, if, in, module, next, nil, not, or, redo, rescue, retry, return, self, super, then, true, undef, unless, until, when, while, yield
Magic variables: __FILE__, __LINE__, __ENCODING__, __method__
Not supported: defined? (use respond_to?, const_defined?, etc. instead), refinements (using, refine).
All standard class and module features are supported: inheritance, include, prepend, extend, attr_reader/attr_writer/ attr_accessor, public/private/protected visibility, class variables (@@var), class methods, and super.
Note: raise without arguments in a rescue clause does not re-raise the current exception. Capture and re-raise explicitly:
Regular expressions require an external gem such as mruby-regexp-pcre or mruby-onig-regexp. Without a regexp gem, Regexp literals (/pattern/) are not available.
Only rightward assignment with simple variable binding is supported:
case/in syntax, array/hash patterns, guard clauses, pin operator, find patterns, and alternative patterns are not supported.
mruby's numeric type sizes depend on the boxing mode and platform.
| Configuration | Range |
|---|---|
| 64-bit word boxing (default on 64-bit) | roughly +/- 2^62 |
| 32-bit word boxing (default on 32-bit) | roughly +/- 2^30 |
| NaN boxing (64-bit only) | -2^31 to 2^31-1 |
Integer overflow raises a RangeError unless the mruby-bigint gem is included, in which case integers automatically promote to arbitrary precision.
By default, Float uses 64-bit double. Compile-time options:
With word boxing on 64-bit, many float values are stored inline (without heap allocation) using a rotation encoding.
These classes are always available in mruby (no gem required):
| Class | Notes |
|---|---|
| Object | Base class for all objects |
| Module | Module definition and mixin |
| Class | Class definition and instantiation |
| NilClass | Singleton nil |
| TrueClass | Singleton true |
| FalseClass | Singleton false |
| Integer | Fixed-precision integer |
| Float | Floating-point (unless MRB_NO_FLOAT) |
| Symbol | Interned identifier |
| String | Mutable byte string |
| Array | Ordered collection |
| Hash | Key-value mapping |
| Range | Interval representation |
| Proc | Closure / callable object |
| Exception | Exception hierarchy root |
| StandardError | Common error base |
| Module | Notes |
|---|---|
| Kernel | Core methods (puts, p, raise, etc.) |
| Comparable | Comparison operators via <=> |
| Enumerable | Collection iteration methods |
mruby's standard library is organized into gemboxes. The default gembox includes all of the below. Use this table to find which gembox provides the class or feature you need:
| Class/Module | Gembox | Gem |
|---|---|---|
| Fiber | stdlib | mruby-fiber |
| Enumerator | stdlib | mruby-enumerator |
| Enumerator::Lazy | stdlib | mruby-enum-lazy |
| Set | stdlib | mruby-set |
| ObjectSpace | stdlib | mruby-objectspace |
| Time | stdlib-ext | mruby-time |
| Struct | stdlib-ext | mruby-struct |
| Data | stdlib-ext | mruby-data |
| Random | stdlib-ext | mruby-random |
| IO, File | stdlib-io | mruby-io |
| Socket | stdlib-io | mruby-socket |
| Dir | stdlib-io | mruby-dir |
| Errno | stdlib-io | mruby-errno |
| Math | math | mruby-math |
| Rational | math | mruby-rational |
| Complex | math | mruby-complex |
| Bigint | math | mruby-bigint |
| Method, UnboundMethod | metaprog | mruby-method |
| Feature | Gembox | Gem |
|---|---|---|
| catch/throw | stdlib | mruby-catch |
| Kernel#sprintf, String#% | stdlib-ext | mruby-sprintf |
| Array#pack, String#unpack | stdlib-ext | mruby-pack |
| Kernel#rand | stdlib-ext | mruby-random |
| Kernel#eval | metaprog | mruby-eval |
| Kernel#binding | metaprog | mruby-binding |
| Proc#binding | metaprog | mruby-proc-binding |
| Runtime compiler | metaprog | mruby-compiler |
The stdlib gembox also extends built-in classes with additional methods. These are included by default:
| Extension | Examples |
|---|---|
| Array extensions | #dig, #union, #difference |
| Hash extensions | #dig, #transform_keys, #transform_values |
| String extensions | #encode, #bytes, #chars |
| Numeric extensions | Integer#digits, Integer#pow |
| Comparable extensions | #clamp |
| Enumerable extensions | #sort_by, #min_by, #max_by, #tally |
| Range extensions | #size, #cover? |
| Proc extensions | #<<, #>> (composition) |
| Symbol extensions | #to_proc |
| Object extensions | #then, #yield_self |
| Kernel extensions | #__method__ |
| Class/Module extensions | Module#name |
| Gembox | Contents | Notes |
|---|---|---|
| stdlib | Core class extensions, Fiber, Enumerator, Set | Works with MRB_NO_STDIO and MRB_NO_FLOAT |
| stdlib-ext | Time, Struct, Data, Random, sprintf, pack | Works with MRB_NO_STDIO and MRB_NO_FLOAT |
| stdlib-io | IO, File, Dir, Socket, Errno | Requires stdio |
| math | Math, Rational, Complex, Bigint | Works with MRB_NO_STDIO |
| metaprog | eval, binding, Method, compiler | Works with MRB_NO_STDIO and MRB_NO_FLOAT |
| default | All of the above + CLI tools | Full installation |
mruby has no require or load. All code (gems, libraries) is linked at build time. To add functionality, include the appropriate gem in your build configuration:
The defined? keyword raises NameError instead of returning a type string or nil. Use alternatives:
Fibers cannot cross C function boundaries. You cannot yield from a fiber inside a C-implemented method. Only mrb_fiber_yield at function return is supported.
Array and String do not support instance variables to reduce memory. This means subclassing Array or String and adding @fields will raise an error.
Operators of primitive classes cannot be overridden by user code. Redefining String#+ has no effect on the behavior of the + operator.
include/prepend/extend do not call append_features/ prepend_features/extend_object hooks. The module is included directly.
For small hashes, #hash is not called on keys. Custom #hash methods may not execute for small hash tables.
Module refinements (refine, using) are not supported.
There is no Encoding class. String encoding is either pure bytes or UTF-8 (opt-in via MRB_UTF8_STRING compile flag).
Redefining nil? has no effect on conditional expressions. The VM uses direct nil checks for performance.
Integer size varies by boxing mode (see Numeric Types above). Code relying on 64-bit integer precision may behave differently on 32-bit or NaN boxing configurations.
Key compile-time macros that affect language behavior:
| Macro | Effect |
|---|---|
| MRB_NO_FLOAT | Remove all float support |
| MRB_USE_FLOAT32 | Use 32-bit float instead of double |
| MRB_UTF8_STRING | Enable UTF-8 string handling |
| MRB_INT32 | Force 32-bit integer |
| MRB_INT64 | Force 64-bit integer |
| MRB_STR_LENGTH_MAX | Max string length (default 1MB) |
| MRB_ARY_LENGTH_MAX | Max array length (default 2^17) |
See mrbconf.md for the complete list of configuration macros.